<template>
  <div class="home">
    <!-- 顶部导航区 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <!-- 自定义插槽 -->
      <van-button @click="$router.push('/search')"  class="search_btn" slot="title" type="info" size="small" icon="search" round>搜索</van-button>
    </van-nav-bar>
    <!-- Tab 标签页 -->
    <van-tabs class="channel_tabs" v-model="active">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name" :name="item.id">
        <!-- 插槽 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <!-- 占位 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" @click="showEditPage" class="hamburger_btn">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 编辑页弹出层 -->
    <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '95%' }"
      close-icon-position="top-left"
      closeable
      round
    >
      <!-- 频道组件 -->
      <article-edit :channel="channels" :active="active" @TOGGLE="changsActive"></article-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/axios";
import ArticleList from "@/components/article/article-lists";
import ArticleEdit from "./components/article-edit";

export default {
  name: "home",
  components: { ArticleList, ArticleEdit },
  data() {
    return {
      active: 0,
      // 全部频道列表
      channels: [],
      // 文章列表数据
      getArticleList: {},
      show: false //弹出层的显示与隐藏
    };
  },
  methods: {
    getChannelsData() {
      // 判断是否为登录状态----
      if (this.$store.state.login.userToken) {
        //登录状态  是则获取自己的频道----
        this.getChannels();
      } else {
        //未登录状态  就查看本地=====本地有数据则直接赋值====否则获取默认频道
        if (window.localStorage.getItem("CHANNELS")) {
          // 有本地频道
          this.channels = JSON.parse(window.localStorage.getItem("CHANNELS"));
        } else {
          this.getChannels();
        }
      }
    },
    // 获取服务器的频道
    async getChannels() {
       const { data: res } = await getUserChannels();
        this.channels = res.data.channels;
        // console.log("获取数据：",res.data.channels)
      // try {
       
      // }
      //  catch (err) {
      //   this.$toast.fail("获取失败！");
      // }
    },
    showEditPage() {
      console.log("ohh");
      this.show = true;
    },
    changsActive(params) {
      // console.log(params);
      this.active = params.id;
      if (!params.status) {
        this.show = false;
      }
    }
  },
  created() {
    this.getChannelsData();
  }
};
</script>

<style scoped lang='less'>
.home {
  padding-top: 172px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset; //取消设置
  }
  .search_btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      color: #fff;
      font-size: 32px;
    }
  }

  .channel_tabs {
    /deep/.van-tabs__wrap {
      position: fixed;
      top: 90px;
      left: 0;
      right: 0;
      z-index: 1;
      //  margin-top: 90px;
    }
    /deep/ .van-tab {
      height: 82px;
      border-right: 1px solid #edeff3;
    }
    /deep/ .van-tabs__nav {
      padding-bottom: 0;
    }
    /deep/ .van-tabs__line {
      width: 31px !important;
      height: 6px;
      bottom: 8px;
      background-color: #3296fa;
    }
    .placeholder {
      //占位用
      flex-shrink: 0; //取消flex的宽度
      width: 66px;
      height: 82px;
    }
    .hamburger_btn {
      position: fixed;
      right: 0;
      //汉堡按钮
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      .iconfont {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 82px;
        background: url(~@/assets/gradient-gray-line.png);
      }
    }
  }
}
</style>